﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using OrganizedIN.DAL;
using OrganizedIN.BAL;
using OrganizedIN.BussinessServices;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;
using OrganizedIN.BAL.Common;
using System.IO;
using System.Diagnostics;

namespace OrganizedIN.UI.Photos
{
    public partial class search_my_photos : System.Web.UI.Page
    {
        string strUserName;
        DataSet ds = new DataSet();
        string genHtml = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                strUserName = Session["UserID"].ToString();
                if (genHtml == "")
                {
                    ddlSortPhotos.Enabled = false;
                }
                else
                {
                    ddlSortPhotos.Enabled = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private string createRatingImages(int rating)
        {
            int empty_stars = 5 - rating;
            string outHtml = string.Empty;
            int flag = 0;
            for (int i = 1; i <= rating; i++)
            {
                flag = 1;
                if (i == 1)
                {
                    outHtml += "<img src='../../images/rating_star.png' alt='" + rating + " Star' style='padding:5px 0 0 6px;' />";
                }
                else
                {
                    outHtml += "<img src='../../images/rating_star.png' alt='" + rating + " Star'/>";
                }
            }
            if (empty_stars > 0)
            {
                for (int i = 1; i <= empty_stars; i++)
                {
                    if (flag != 1)
                    {
                        outHtml += "<img src='../../images/rating_star_grey.png' alt='" + rating + " Star' style='padding:5px 0 0 6px;' />";
                    }
                    else
                    {
                        outHtml += "<img src='../../images/rating_star_grey.png' alt='" + rating + " Star'/>";
                    }
                }
            }
            return outHtml;
        }

        protected void btnsearch_Click1(object sender, EventArgs e)
        {
            try
            {
                if (txtByEvent.Text == "" && txtBySharing.Text == "" && txtByTitle.Text == "" && simple_calendar.Text == "" && ddlRating.SelectedValue == "")
                {
                    pmsg.InnerText = "Please provide atleast one searching parameter";
                    wrapper.InnerHtml = "";
                }
                else
                {
                    pmsg.InnerText = "";
                    DataSet ds = new DataSet();

                    ds = Common.getPhotoSearchList(strUserName, txtByTitle.Text, txtByEvent.Text, txtBySharing.Text, ddlRating.SelectedValue.ToString(), simple_calendar.Text);

                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        string imgFile = string.Empty;
                        string genHtml = "";
                        foreach (DataRow row in ds.Tables[0].Rows)
                        {
                            genHtml += "<div class='dvspace'>";
                            genHtml += "<p class='p-video'>";
                            imgFile = row[0].ToString();
                            /*if (row[0].ToString().IndexOf("www.youtube-nocookie.com") == -1)
                            {
                                imgFile = row[0].ToString();
                                imgFile = imgFile.Substring(0, imgFile.Length - 3) + "jpg";
                            }
                            else
                            {
                                imgFile = "../../images/youtube.jpg";

                            }*/

                            genHtml += "<img class='img-video' src='" + imgFile + "' alt='" + row[1] + "' style='float:left;' />";
                            genHtml += "<span style='font-weight:bold; margin-left:109px;'>" + row[1] + "</span>";
                            genHtml += "<span style='margin-left:109px;'>" + DateTime.Parse(row[2].ToString()).ToLongDateString() + "</span>";
                            genHtml += "<span>";
                            genHtml += createRatingImages(Convert.ToInt32(row[3]));
                            genHtml += "</span>";
                            genHtml += "</p>";
                            genHtml += "</p>";
                            genHtml += "<p class='p-option'><a href='#'>Summary</a>|<a href='#'>Download</a>|<a href='rename_photos.aspx?id=" + row[4] + "'>Rename</a>|<a href='#'>Share(d)</a>|<a href='#'>Archive</a>|<br /><a href='#'>Add To Album</a>|<a href='add_rating_photos.aspx?id=" + row[4] + "'>Assign Rating</a>|<a href='redate_photos.aspx?id=" + row[4] + "'>ReDate</a>|<a href='#" + row[4] + "'>Update Event</a>|<a href='delete_photos.aspx?id=" + row[4] + "&param=del' onclick='confirm('Are you sure you want to delete this item?');'>Delete</a></p>";
                            genHtml += "</div>";
                            genHtml += "<div style='clear:both;'></div>";
                        }
                        wrapper.InnerHtml = genHtml;
                        ddlSortPhotos.Enabled = true;
                    }
                    else
                    {
                        wrapper.InnerHtml = "<h2>No record(s) found</h2>";
                        ddlSortPhotos.Enabled = false;
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void ddlSortPhotos_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                ds = Common.getPhotoSortResult(strUserName, txtByTitle.Text, txtByEvent.Text, txtBySharing.Text, ddlRating.SelectedValue.ToString(), simple_calendar.Text, ddlSortPhotos.SelectedValue.ToString());

                if (ds.Tables[0].Rows.Count > 0)
                {
                    string imgFile = string.Empty;
                    foreach (DataRow row in ds.Tables[0].Rows)
                    {
                        genHtml += "<div class='dvspace'>";
                        genHtml += "<p class='p-video'>";
                        imgFile = row[0].ToString();
                        /*if (row[0].ToString().IndexOf("www.youtube-nocookie.com") == -1)
                        {
                            imgFile = row[0].ToString();
                            imgFile = imgFile.Substring(0, imgFile.Length - 3) + "jpg";
                        }
                        else
                        {
                            imgFile = "../../images/youtube.jpg";

                        }*/

                        genHtml += "<img class='img-video' src='" + imgFile + "' alt='" + row[1] + "' style='float:left;' />";
                        genHtml += "<span style='font-weight:bold; margin-left:109px;'>" + row[1] + "</span>";
                        genHtml += "<span style='margin-left:109px;'>" + DateTime.Parse(row[2].ToString()).ToLongDateString() + "</span>";
                        genHtml += "<span>";
                        genHtml += createRatingImages(Convert.ToInt32(row[3]));
                        genHtml += "</span>";
                        genHtml += "</p>";
                        genHtml += "</p>";
                        genHtml += "<p class='p-option'><a href='#'>Summary</a>|<a href='#'>Download</a>|<a href='rename_photos.aspx?id=" + row[4] + "'>Rename</a>|<a href='#'>Share(d)</a>|<a href='#'>Archive</a>|<br /><a href='#'>Add To Album</a>|<a href='add_rating_photos.aspx?id=" + row[4] + "'>Assign Rating</a>|<a href='redate_photos.aspx?id=" + row[4] + "'>ReDate</a>|<a href='#" + row[4] + "'>Update Event</a>|<a href='delete_photos.aspx?id=" + row[4] + "&param=del' onclick='confirm('Are you sure you want to delete this item?');'>Delete</a></p>";
                        genHtml += "</div>";
                        genHtml += "<div style='clear:both;'></div>";
                    }
                    wrapper.InnerHtml = genHtml;
                    ddlSortPhotos.Enabled = true;
                }
                else
                {
                    wrapper.InnerHtml = "<h2>No record(s) found</h2>";
                    ddlSortPhotos.Enabled = false;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
